package com.wzy.media.api;


import com.wzy.base.exception.CustomException;
import com.wzy.base.model.PageParams;
import com.wzy.base.model.PageResult;
import com.wzy.base.model.RestResponse;
import com.wzy.media.model.dto.QueryMediaParamsDto;
import com.wzy.media.model.dto.UploadFileParamsDto;
import com.wzy.media.model.dto.UploadFileResultDto;
import com.wzy.media.model.po.MediaFiles;
import com.wzy.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @description 媒资文件管理接口
 * @author wzy
 * @date 2024/1/11 20:29
 * @version 1.0
 */
 @Api(value = "媒资文件管理接口",tags = "媒资文件管理接口")
 @RestController
public class MediaFilesController {
    @Autowired
    MediaFileService mediaFileService;
    @ApiOperation("媒资列表查询接口")
    @PostMapping("/files")
    public PageResult<MediaFiles> list(PageParams pageParams, @RequestBody QueryMediaParamsDto queryMediaParamsDto){
        Long companyId = 1232141425L;
        return mediaFileService.queryMediaFiels(companyId,pageParams,queryMediaParamsDto);
    }

    /**
     * consumes直译是消费者，其实就是限制方法处理指定Content-Type的http请求
     * multipart/form-data ： 需要在表单中进行文件上传时，就需要使用该格式
     * @param upload
     * @return
     * @throws IOException
     */
    @ApiOperation("上传文件")
    @RequestMapping(value = "/upload/coursefile",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public UploadFileResultDto upload(@RequestPart("filedata") MultipartFile upload,@RequestParam(value= "objectName",required=false) String objectName) throws IOException {
        //Long companyId, UploadFileParamsDto uploadFileParamsDto, String localFilePath
        Long companyId = 1232141425L;

        //准备上传文件的信息
        UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();
        //原始文件名称
        uploadFileParamsDto.setFilename(upload.getOriginalFilename());
        //文件大小
        uploadFileParamsDto.setFileSize(upload.getSize());
        //文件类型
        uploadFileParamsDto.setFileType("001001");//001001代表图片
        //文件标签
        uploadFileParamsDto.setTags("课程图片");

        //创建一个临时文件
        File tempFile = File.createTempFile("minio", ".temp");
        //上传的文件拷贝到临时文件
        upload.transferTo(tempFile);
        //文件路径
        String localFilePath = tempFile.getAbsolutePath();
        UploadFileResultDto uploadFileResultDto = mediaFileService.uploadFile(companyId, uploadFileParamsDto, localFilePath,objectName );
        return uploadFileResultDto;
    }

    @ApiOperation("预览文件")
    @GetMapping("/preview/{mediaId}")
    public RestResponse<String> getPlayUrlByMediaId(@PathVariable String mediaId) {

        MediaFiles mediaFiles = mediaFileService.getFileById(mediaId);
        if (mediaFiles == null || StringUtils.isEmpty(mediaFiles.getUrl())) {
            throw new CustomException("视频还没有转码处理");
        }
        return RestResponse.success(mediaFiles.getUrl());

    }
    @ApiOperation("删除文件")
    @DeleteMapping("/{mediaId}")
    public void deleteMediaByMediaId(@PathVariable String mediaId) {
        mediaFileService.deleteFileByMediaId(mediaId);
    }


}
